package main
import (
	"fmt"
	"net/http"
	"io"
	"os"
	"io/ioutil"
)

var file,err=os.Create("./res.log")
var i int=0
func listenhttp() {
        http.HandleFunc("/", Cop)
        err := http.ListenAndServe(":9090", nil)
        if err != nil {
                fmt.Println(err)
                return
        }
}

func Cop(w http.ResponseWriter,r *http.Request) {
	fmt.Println("method:",r.Method)
	if r.Method=="GET" {
		fmt.Printf("method is get!\n")
	}else {
		//r.ParseMultipartForm(32<<20)
		fmt.Printf("start formfile.\n")
		file,handler,err:=r.FormFile("uploadfile")
		fmt.Printf("end formfile.\n")
		if err!=nil {
			fmt.Println(err)
			return
		}
		defer file.Close()
		fmt.Printf("start openfile.\n")
		tfile:=fmt.Sprintf(handler.Filename)
		i=i+1
		f,err:=os.OpenFile(tfile,os.O_WRONLY|os.O_CREATE,0666)
		fmt.Printf("end openfile.\n")
		if err!=nil {
			fmt.Println(err)
			return
		}
		defer f.Close()
		fmt.Printf("start copy.\n")
		io.Copy(f,file)
		fmt.Printf("end copy.\n")
	}
}
	
	
func Res(w http.ResponseWriter, r *http.Request) {
		 defer r.Body.Close()
	         data, err := ioutil.ReadAll(r.Body)
        	 if err != nil {
                	w.Write([]byte("0001"))
               	        return
        	}
		_,err1:=io.WriteString(file,string(data))
		if err1!=nil {
			return
		}
                w.Write([]byte("0000"))
}
func main(){
	listenhttp()
}
